/* mapping.h - 1992/07/19 */

/* It is usually better to include "lpc_incl.h" instead of including this
   directly */

#ifndef _MAPPING_H
#define _MAPPING_H

#define MAP_POINTER_HASH(x) (((POINTER_INT)x) >> 4)

typedef struct mapping_node_s {
    struct mapping_node_s *next;
    svalue_t values[2];
} mapping_node_t;

#define MAP_HASH_TABLE_SIZE 8	/* must be a power of 2 */

#define MAP_LOCKED 0x80000000
#define MAP_COUNT(m) ((m)->count & ~MAP_LOCKED)

typedef struct mapping_s {
    unsigned short ref;		/* how many times this map has been
				 * referenced */
#ifdef DEBUG
    int extra_ref;
#endif
    mapping_node_t **table;	/* the hash table */
    unsigned short table_size;	/* # of buckets in hash table == power of 2 */
    unsigned short unfilled;	/* # of buckets among 80% of total buckets that do not have entries */
    unsigned int count;		/* total # of nodes actually in mapping  */
} mapping_t;

#define mapping_too_large() \
    error("Mapping exceeded maximum allowed size of %d.\n",MAX_MAPPING_SIZE);

#ifndef max
#define max(x,y) ((x) > (y)) ? (x) : (y)
#endif

/*
 * mapping.c
 */
extern int num_mappings;
extern int total_mapping_size;
extern int total_mapping_nodes;
extern mapping_node_t *locked_map_nodes;
extern mapping_node_t *free_nodes;

int msameval(svalue_t *, svalue_t *);
INLINE mapping_t *load_mapping_from_aggregate(svalue_t *, int);
INLINE mapping_t *allocate_mapping(int);
INLINE void free_mapping(mapping_t *);
INLINE svalue_t *find_in_mapping(mapping_t *, svalue_t *);
INLINE svalue_t *find_for_insert(mapping_t *, svalue_t *, int);
INLINE void absorb_mapping(mapping_t *, mapping_t *);
INLINE mapping_t *add_mapping(mapping_t *, mapping_t *);
mapping_node_t *new_map_node(void);
int growMap(mapping_t *);
void unlock_mapping(mapping_t *);
INLINE mapping_t *compose_mapping(mapping_t *, mapping_t *, unsigned short);
array_t *mapping_indices(mapping_t *);
void dealloc_mapping(mapping_t *);

void add_mapping_pair(mapping_t *, char *, int);
void add_mapping_string(mapping_t *, char *, char *);
void add_mapping_malloced_string(mapping_t *, char *, char *);
void add_mapping_object(mapping_t *, char *, object_t *);
void add_mapping_array(mapping_t *, char *, array_t *);

#endif				/* _MAPPING_H */
